Re: [GENERAL] Any ideas why this doesn't work or how to rewrite it? - Mailing list pgsql-general

From Herouth Maoz
Subject Re: [GENERAL] Any ideas why this doesn't work or how to rewrite it?
Date
Msg-id l03130309b34e25d54cfd@[147.233.159.109]
Whole thread Raw
In response to Re: [GENERAL] Any ideas why this doesn't work or how to rewrite it?  ("Brett W. McCoy" <bmccoy@lan2wan.com>)
List pgsql-general
At 21:04 +0300 on 29/04/1999, Brett W. McCoy wrote:


> I think, Aaron, you could get a count of distinct customer names like this:
>
> SELECT DISTINCT customer_username, COUNT(*) FROM customerdata
> GROUP BY customer_username;
>
> This will give you 2 columns, one with the distinct customer_usernames
> and the second with the count of each.  The GROUP BY caluse is important
> here.  This looks like what you wanted in your original query.

No, Brett. COUNT( DISTINCT ...) is supposed to count the number of distinct
names in a table. Here, I created a test table:

testing=> select * from test;
customer
--------
moshe
david
hanna
david
sarah
moshe
suzanne
moshe
moshe
(9 rows)

The distinct names are:

testing=> select distinct customer
testing-> from test;
customer
--------
david
hanna
moshe
sarah
suzanne
(5 rows)

So clearly, the datum he wanted was "5" - there are five distinct customers
here.

Your query, however, gives the following:

testing=> select distinct customer, count(*)
testing-> from test
testing-> group by customer;
customer|count
--------+-----
david   |    2
hanna   |    1
moshe   |    4
sarah   |    1
suzanne |    1
(5 rows)

Which shows him the number of REPETITIONS on each distinct name.

My ugly query gives:

testing=> select count(*)
testing-> from test t1
testing-> where int( oid ) = (
testing->   SELECT min( int( t2.oid ) )
testing->   FROM test t2
testing->   WHERE t2.customer = t1.customer
testing-> );
count
-----
    5
(1 row)

And this is the exact number of distinct names in the table.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma



pgsql-general by date:

Previous
From: "Brett W. McCoy"
Date:
Subject: Re: [GENERAL] Any ideas why this doesn't work or how to rewrite it?
Next
From: "Martin Wong"
Date:
Subject: RE: [GENERAL] LIMIT QUESTION